﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Model.Sadharon;
using InventorySys.controller;

namespace InventorySys.view
{
    public partial class CustomerInfo : Form
    {
        #region private method
        /// <summary>
        /// private method
        /// </summary>
        Result result = new Result();
        List<Model.Upadan.Customer> listCustomer = new List<Model.Upadan.Customer>();
        private CustomerController customerController;
        private Model.Upadan.Customer customer;
        
        #endregion

        #region constructor
        /// <summary>
        /// constructor
        /// </summary>
        public CustomerInfo()
        {
            InitializeComponent();
            customerController = new CustomerController();
        }
        #endregion

        #region customer list loaded
        /// <summary>
        /// CustomerInfo_Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CustomerInfo_Load(object sender, EventArgs e)
        {
            customerListLoad();
        }
        
        /// <summary>
        /// supplier list load 
        /// </summary>
        private void customerListLoad()
        {
            result = customerController.GetAllCustomer();;
            if (result.IsSuccess)
            {
                listCustomer = (List<Model.Upadan.Customer>)result.ResultObj;
                BindCustomerList(listCustomer);
            }
        }

        /// <summary>
        /// BindCustomerList
        /// </summary>
        /// <param name="listSupplier"></param>
        private void BindCustomerList(List<Model.Upadan.Customer> listCustomer)
        {
            customer = new Model.Upadan.Customer();
            DtGrdCuList.Rows.Clear();
            for (int i = 0; i < listCustomer.Count; i++)
            {
                customer = listCustomer[i];
                DtGrdCuList.Rows.Add();
                DtGrdCuList.Rows[i].Cells[0].Value = customer.Id;
                DtGrdCuList.Rows[i].Cells[1].Value = customer.AccountReference;
                DtGrdCuList.Rows[i].Cells[2].Value = customer.CompanyName;
                DtGrdCuList.Rows[i].Cells[3].Value = customer.ContactPerson;
                DtGrdCuList.Rows[i].Cells[4].Value = "Edit";
                DtGrdCuList.Rows[i].Cells[5].Value = "Delete";
            }
        }
        #endregion

        #region datagrid cell content click
        /// <summary>
        /// datagrid cell content click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DtGrdCuList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int i, j;
            i = int.Parse(e.RowIndex.ToString());
            j = int.Parse(e.ColumnIndex.ToString());
            if (j == 4)
            {
                string id = DtGrdCuList.Rows[i].Cells[0].Value.ToString();
                Customer customerForm = new Customer();
                customerForm.decidePageMode("viewMode", id);
                customerForm.Show(this);
            }
            else if (j == 5)
            {
                string id = DtGrdCuList.Rows[i].Cells[0].Value.ToString();
                result = customerController.deleteCustomerDetails(id);
                if (result.IsSuccess)
                {
                    MessageBox.Show(result.Message);
                    customerListLoad();
                }
                else
                {
                    MessageBox.Show(result.Message);
                }

            }
        }
        #endregion

    }
}
